define(function(require, exports, module) {
	"require:nomunge,exports:nomunge,module:nomunge";
	
	
	var validate = require("../../custom/base/component-validate.js");
	
	var base = require("../../custom/base/component-base.js");
	
	base.init(base);
		
	var async = require("../../custom/base/component-async.js");
	
	exports.init=function(m) {
		
		window.$ldh_location = m;
		
	
	};
	
	//初始化百度地图
	var localSearch="";
	exports.initMap=function(){
		
		var map = new BMap.Map("l-map");
		
		var options = {
				onSearchComplete: function(results){
					// 判断状态是否正确
				
					if (localSearch.getStatus() == BMAP_STATUS_SUCCESS){
						
						var htmlStr="<dl><dt>搜索结果</dt>";
						
						for (var i = 0; i < results.getCurrentNumPois(); i ++){
							 var poi = results.getPoi(i);
							 var title=base.replaceQuotationMarks(results.getPoi(i).title);
							
							//$("#ldhLocation .location_history").append('<li lat="'+poi.point.lat+'" lng="'+poi.point.lng+'" onclick=$ldh_base.selectSearchItem("'+title+'","'+poi.point.lng+'","'+poi.point.lat+'")>'+results.getPoi(i).title+'</li>');
							htmlStr+='<dd onclick=$ldh_location.setSearchAddress("'+poi.point.lng+'","'+poi.point.lat+'","'+title+'")><p>'+results.getPoi(i).title+'</p></dd>';
							
						}
							htmlStr+='</dl>';
							$("#ldhLocation .search_history").hide();
							$("#ldhLocation .search_current").show();
							$("#ldhLocation .search_current").html(htmlStr);
							
						
						if(results.getCurrentNumPois()>0){
						}
					}else{
						
						showTips("找不到该地址");
					}
					
					base.cancelLoading();
				}
			};

		localSearch = new BMap.LocalSearch(map, options);
		
		
		
	};
	
	//搜索地址
	exports.searchAddress=function(){
		
		var str=$("#ldhLocation input[name='address']").val();
		
		if(validate.isNull(str))
			return;
		
		base.showLoading();
		
		localSearch.search(base.getCookie("city")+str);
		
	}
	
	//设置搜索地址
	exports.setSearchAddress=function(lng,lat,address){
		base.setCookie("lng",lng);
		base.setCookie("lat",lat);
		base.setCookie("location",address);
		$("#ldhHome .header_fixed h1 a").html(address);
		$("#ldhLocation .location_top span:eq(0)").html(address);
		
		exports.saveSearchHistoryCookie(address,lng,lat);
		
		
		//window.location.href="#ldhHome";
		window.location.href=ctx+"/h5/index.shtml";
	}
	
	//设置历史地址
	exports.setHistoryAddress=function(lng,lat,address){
		base.setCookie("lng",lng);
		base.setCookie("lat",lat);
		base.setCookie("location",address);
		$("#ldhHome .header_fixed h1 a").html(address);
		$("#ldhLocation .location_top span:eq(0)").html(address);

		//window.location.href="#ldhHome";
		
		window.location.href=ctx+"/h5/index.shtml";
	}
	
	//设置城市
	exports.setCity=function(city){
		
		base.setCookie("city",city);
		
		$("#ldhLocation .header_fixed em").html(city);
		
		//window.location.href="#ldhLocation";
		
		window.location.href=ctx+"/h5/location.shtml";
	};
	
	
	//保存搜索历史
	exports.saveSearchHistoryCookie=function(name,lng,lat) {
		try {
			
			var cookieList;
			var cookie_item;
			
			if(validate.isNull(base.getCookie("cookieList")) || typeof(base.getCookie("cookieList"))=="undefined"){
				//如果历史记录为空,新家一条记录
				cookieList=new Array();
				cookie_item = new Object();
				cookie_item.name=name;
				cookie_item.lng=lng;
				cookie_item.lat=lat;
				cookieList.push(cookie_item);
				base.setCookie("cookieList",JSON.stringify(cookieList));

				
			}else{
				
				
				var cookieListData=JSON.parse(base.getCookie("cookieList"));
				
				cookieList=new Array();
				
				var isExits=false;
				
				for(var i=0;i<cookieListData.length;i++){
										
					if(name!=cookieListData[i].name){
						cookie_item = new Object();
						cookie_item.name=cookieListData[i].name;
						cookie_item.lng=cookieListData[i].lng;
						cookie_item.lat=cookieListData[i].lat;
						cookieList.push(cookie_item);
						
					}else{
						
						isExits=true;
					}
				}
				
				//如果历史记录不存在这条记录, 则把该记录添加进去
				if(!isExits){
					
					cookie_item = new Object();
					cookie_item.name=name;
					cookie_item.lng=lng;
					cookie_item.lat=lat;
					cookieList.push(cookie_item);
				}
				
				base.setCookie("cookieList",JSON.stringify(cookieList));

				
			}
			
			

		} catch (e) {
		}
		
		exports.getSearchHistoryCookie();
	};
	
	//获取最近搜索列表
	exports.getSearchHistoryCookie=function() {
		
		try {

			
			if(!validate.isNull(base.getCookie("cookieList"))){
				
				var cookieListData=JSON.parse(base.getCookie("cookieList"));
				
				var length=cookieListData.length;
				
				if(length>10)
					length=10;
				
				var htmlStr="<dl><dt>搜索历史</dt>";
				
				for(var i=0;i<length;i++){
					
						var name=base.replaceQuotationMarks(cookieListData[i].name);
						
						htmlStr+='<dd onclick=$ldh_location.setHistoryAddress("'+cookieListData[i].lng+'","'+cookieListData[i].lat+'","'+name+'")><p>'+name+'</p></dd>';

				}
				
					htmlStr+='</dl>';
					$("#ldhLocation .search_history").show();
					$("#ldhLocation .search_current").hide();
					$("#ldhLocation .search_history").html(htmlStr);
					
				
				
				
				
			}
			

		} catch (e) {

		}

	};
});